<template>
	<view class="container">
		<view class="ywyj-body">
		    <uni-list :border="true">
				<template v-for="(item, index) in yjlbArr">
					<uni-list-chat :clickable="true" :avatar-circle="true" :title="item.fileName"
					:avatar="loadUrl + item.filePath" @click="previewImage(index)"
					:note="item.fileType" :time="item.createTime" ></uni-list-chat>
				</template>
		    </uni-list>
		</view>
		<uni-fab v-show="!isDisabled" ref="fab" horizontal="right" @fabClick="toAdd" />
	</view>
</template>



<script>
	import storage from '@/utils/storage'
	import config from '@/config'
	import { sfkybjData } from "@/api/trms/trms"
	import { getYwyj } from "@/api/trms/query"
	export default {
		data() {
			return {
				patientId: null,
				isDisabled: false,
				userId: null,
				loadUrl: config.baseUrl,
				yjlbArr: [],
				imgList: [],
				fileList: []
			}
		},
		onLoad(option){
			this.patientId = option.patientId;
			this.userId = option.userId;
			console.log("进入附件列表页面后、参数：",this.patientId)
			if(this.patientId){
				// 查询附件
				this.getYwyjFun();
			}
			sfkybjData(this);
			this.eventChannel = this.getOpenerEventChannel();
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			this.eventChannel.on('listPageData', function(data) {
				
			});	
		},
		methods: {
			getYwyjFun(){
				console.log('执行查询')
				let that = this;
				getYwyj(this.patientId).then(r => {
					this.yjlbArr = r.data;
					r.data.forEach(b=>{
						let file = {};
						that.imgList.push(that.loadUrl+b.filePath);
						let fileType = b.filePath.substring(b.filePath.lastIndexOf(".")+1);
						b.fileType = fileType;
						if(fileType.toUpperCase() == "PDF"){
							file.type = '2';
							file.name = "预览pdf";
						}else{
							file.type = '1';
							file.name = "预览图片";
						}
						file.src = that.loadUrl+b.filePath;
						this.fileList.push(file);
					});
				})
			},
			toPage(item){
				if(item.type === '2'){//预览文档
					//#ifdef MP-WEIXIN
					//微信小程序预览文档可以直接打开，不用跳转页面
					this.previewWechat(item.src)
					return
					//#endif
				}
				uni.navigateTo({
					url:'/pages/trms/patient/pdf?item='+encodeURIComponent(JSON.stringify(item))
				})
			},
			// 微信小程序预览文档,可预览.doc,.docx,.xls,.xlsx,.pdf等文件
			previewWechat(urlPdf) {
				uni.showLoading({
					title: '正在加载中..'
				})
				uni.downloadFile({
					url: urlPdf,
					success: function(res) {
						var filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function(res) {
								console.log('打开文档成功');
								uni.hideLoading()
							},
						});
					},
					complete: function(r) {
						uni.hideLoading()
					}
				});
			},
			previewImage(index) {
				console.log(index)
				const url = this.imgList[index];
				let fileType = url.substring(url.lastIndexOf(".")+1);
				console.log("fileType"+fileType)
				if(fileType.toUpperCase() == "PDF"){
					// this.openPdf(url,yjlbArr[index].filePath);
					this.toPage(this.fileList[index]);
				}else{
					uni.previewImage({
						current: index, // 当前显示图片索引
						urls: this.imgList // 需要预览的图片http链接列表
					});
				}
			},
			toAdd(){
				let that = this;
				uni.navigateTo({
					url: '/pages/trms/patient/ywyj?patientId='+that.patientId,
					events: {
					    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					    listPageData: function(data) {
						  if(data && data.data == 'refPage'){
							  // 重新查询
							  that.getYwyjFun();
						  }
					    }
					  },
					  success: function(res) {
					    // 通过eventChannel向被打开页面传送数据
					    res.eventChannel.emit('listPageData', { data: 'data from starter page',patientId: that.patientId })
					  }
				});
			}
		}
	}
</script>

<style>
	page {
	  background-color: #ffffff;
	}
	
	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	
	.container .ywyj-body .example-body{
		padding: 20px;
	}
</style>
